package com.study.juc.thread;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @program: juc-study
 * @author: jzhou
 * @date: 2022-11-28 14:01
 * @version: 1.0
 * @description: 循环栅栏
 **/
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        // 当 达到 给定的 屏障值时，触发 Runnable 接口线程执行
        int NUMBER = 7;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUMBER, () -> {
            System.out.println("*******");
        });

        for (int i = 1; i <= NUMBER; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() + "星龙珠已被收集");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
